package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.Protocol;
import com.hejia.alauda.service.ProductService;
import com.hejia.alauda.utils.HttpResponse;

@Controller
@RequestMapping("protocols")
public class ProtocolController {
	
	@Resource
	private ProductService protocolService;
	
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(Protocol protocol) {
		HttpResponse response = new HttpResponse();
		try{
			protocol.setCreateDate(new Date());
			protocolService.addProtocol(protocol);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", protocol.getId());
			map.put("title", protocol.getTitle());
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("添加协议成功");
			response.setData(map);
		}catch(Exception e){
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("添加协议失败");
			response.setData("");
		}
		
		return response;
	}
	
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse delete(@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try{
			Protocol protocol = new Protocol();
			protocol.setId(id);
			this.protocolService.deleteProtocol(protocol);
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("删除协议成功");
			response.setData(id);
		}catch(Exception e){
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("删除协议失败");
			response.setData("");
		}
		
		return response;
	}
	
	@RequestMapping(value = "showupdate/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse showupdate(@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try{
			Protocol protocol = protocolService.findProtocolById(id);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", protocol.getId());
			map.put("title", protocol.getTitle());
			map.put("content", protocol.getContent());
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("修改协议成功");
			response.setData(map);
		}catch(Exception e){
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("修改协议失败");
			response.setData("");
		}
		
		return response;
	}
	
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse update(Protocol protocol,@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try{
			Protocol protocol2 = protocolService.findProtocolById(id);
			protocol2.setContent(protocol.getContent());
			protocol2.setTitle(protocol.getTitle());
			protocol2.setModifyDate(new Date());
			protocolService.modifyProtocol(protocol2);
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", protocol2.getId());
			map.put("title", protocol.getTitle());
			response.setCode("201");
			response.setStatus("success");
			response.setMessage("修改协议成功");
			response.setData(map);
		}catch(Exception e){
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("修改协议失败");
			response.setData("");
		}
		
		return response;
	}

}
